home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / colorsys.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  3.9 KB  |  162 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Conversion functions between RGB and other color systems.
  5.  
  6. This modules provides two functions for each color system ABC:
  7.  
  8.   rgb_to_abc(r, g, b) --> a, b, c
  9.   abc_to_rgb(a, b, c) --> r, g, b
  10.  
  11. All inputs and outputs are triples of floats in the range [0.0...1.0].
  12. Inputs outside this range may cause exceptions or invalid outputs.
  13.  
  14. Supported color systems:
  15. RGB: Red, Green, Blue components
  16. YIQ: used by composite video signals
  17. HLS: Hue, Luminance, Saturation
  18. HSV: Hue, Saturation, Value
  19. '''
  20. __all__ = [
  21.     'rgb_to_yiq',
  22.     'yiq_to_rgb',
  23.     'rgb_to_hls',
  24.     'hls_to_rgb',
  25.     'rgb_to_hsv',
  26.     'hsv_to_rgb']
  27. ONE_THIRD = 1.0 / 3.0
  28. ONE_SIXTH = 1.0 / 6.0
  29. TWO_THIRD = 2.0 / 3.0
  30.  
  31. def rgb_to_yiq(r, g, b):
  32.     y = 0.29999999999999999 * r + 0.58999999999999997 * g + 0.11 * b
  33.     i = 0.59999999999999998 * r - 0.28000000000000003 * g - 0.32000000000000001 * b
  34.     q = (0.20999999999999999 * r - 0.52000000000000002 * g) + 0.31 * b
  35.     return (y, i, q)
  36.  
  37.  
  38. def yiq_to_rgb(y, i, q):
  39.     r = y + 0.94826200000000005 * i + 0.62401300000000004 * q
  40.     g = y - 0.27606599999999998 * i - 0.63980999999999999 * q
  41.     b = (y - 1.10545 * i) + 1.72986 * q
  42.     if r < 0.0:
  43.         r = 0.0
  44.     
  45.     if g < 0.0:
  46.         g = 0.0
  47.     
  48.     if b < 0.0:
  49.         b = 0.0
  50.     
  51.     if r > 1.0:
  52.         r = 1.0
  53.     
  54.     if g > 1.0:
  55.         g = 1.0
  56.     
  57.     if b > 1.0:
  58.         b = 1.0
  59.     
  60.     return (r, g, b)
  61.  
  62.  
  63. def rgb_to_hls(r, g, b):
  64.     maxc = max(r, g, b)
  65.     minc = min(r, g, b)
  66.     l = (minc + maxc) / 2.0
  67.     if minc == maxc:
  68.         return (0.0, l, 0.0)
  69.     
  70.     if l <= 0.5:
  71.         s = (maxc - minc) / (maxc + minc)
  72.     else:
  73.         s = (maxc - minc) / (2.0 - maxc - minc)
  74.     rc = (maxc - r) / (maxc - minc)
  75.     gc = (maxc - g) / (maxc - minc)
  76.     bc = (maxc - b) / (maxc - minc)
  77.     if r == maxc:
  78.         h = bc - gc
  79.     elif g == maxc:
  80.         h = 2.0 + rc - bc
  81.     else:
  82.         h = 4.0 + gc - rc
  83.     h = h / 6.0 % 1.0
  84.     return (h, l, s)
  85.  
  86.  
  87. def hls_to_rgb(h, l, s):
  88.     if s == 0.0:
  89.         return (l, l, l)
  90.     
  91.     if l <= 0.5:
  92.         m2 = l * (1.0 + s)
  93.     else:
  94.         m2 = l + s - l * s
  95.     m1 = 2.0 * l - m2
  96.     return (_v(m1, m2, h + ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h - ONE_THIRD))
  97.  
  98.  
  99. def _v(m1, m2, hue):
  100.     hue = hue % 1.0
  101.     if hue < ONE_SIXTH:
  102.         return m1 + (m2 - m1) * hue * 6.0
  103.     
  104.     if hue < 0.5:
  105.         return m2
  106.     
  107.     if hue < TWO_THIRD:
  108.         return m1 + (m2 - m1) * (TWO_THIRD - hue) * 6.0
  109.     
  110.     return m1
  111.  
  112.  
  113. def rgb_to_hsv(r, g, b):
  114.     maxc = max(r, g, b)
  115.     minc = min(r, g, b)
  116.     v = maxc
  117.     if minc == maxc:
  118.         return (0.0, 0.0, v)
  119.     
  120.     s = (maxc - minc) / maxc
  121.     rc = (maxc - r) / (maxc - minc)
  122.     gc = (maxc - g) / (maxc - minc)
  123.     bc = (maxc - b) / (maxc - minc)
  124.     if r == maxc:
  125.         h = bc - gc
  126.     elif g == maxc:
  127.         h = 2.0 + rc - bc
  128.     else:
  129.         h = 4.0 + gc - rc
  130.     h = h / 6.0 % 1.0
  131.     return (h, s, v)
  132.  
  133.  
  134. def hsv_to_rgb(h, s, v):
  135.     if s == 0.0:
  136.         return (v, v, v)
  137.     
  138.     i = int(h * 6.0)
  139.     f = h * 6.0 - i
  140.     p = v * (1.0 - s)
  141.     q = v * (1.0 - s * f)
  142.     t = v * (1.0 - s * (1.0 - f))
  143.     if i % 6 == 0:
  144.         return (v, t, p)
  145.     
  146.     if i == 1:
  147.         return (q, v, p)
  148.     
  149.     if i == 2:
  150.         return (p, v, t)
  151.     
  152.     if i == 3:
  153.         return (p, q, v)
  154.     
  155.     if i == 4:
  156.         return (t, p, v)
  157.     
  158.     if i == 5:
  159.         return (v, p, q)
  160.     
  161.  
  162.